# -*- coding: utf-8 -*-

"""
@author: Laowang
@contact: QQ:1125564921
@Created on: 2022/6/22
@Remark: 
"""

import time
from functools import total_ordering, wraps


@total_ordering
class TimeSince:
    """ time operator """

    def __init__(self, ot: int = None, time_func=time.perf_counter) -> None:
        self._time_func = time_func
        self._ot = ot or self._time_func()
        self.spare_time = 0.0

    def sub(self, nt: int = None) -> int:
        nt = time.perf_counter()
        return nt - self._ot

    def __le__(self, __o: object):
        return self._ot <= __o._ot

    def __eq__(self, __o: object) -> bool:
        return self._ot == __o._ot

    def __ge__(self, __o: object):
        return self._ot >= __o._ot

    def __enter__(self):
        self.spare_time = self._time_func()

    def __exit__(self, exc_type, exc_val, exc_tb):
        self.spare_time = self._time_func() - self.spare_time
        print("[timeit] ", self.spare_time)

    def since(self) -> int:
        nt = time.time()
        return nt - self._ot


# Cost time
def StatisticalTime(func):
    @wraps(func)
    def inner(*args, **kwargs):
        t = TimeSince(time.time())
        res = func(*args, **kwargs)
        print("[*] Cost time >>>", t.since())
        return res
    return inner


class CounterOperator:
    """ Call counter """
    pass
